package com.diligreat.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.diligreat.cache.Cache;
import com.diligreat.domain.Lable;
import com.diligreat.mapper.LableMapper;

@Service
public class NavigationService {
	@Autowired
	private LableMapper lableMapper;
	
	
	/**
	 * 返回导航栏的字符数组
	 * @return
	 */
	public String getNavigationString()
	{
		if(Cache.getCache().getNavigationString()==null)
		{
			return this.reloadNavigationString();
		}
		
		if(Cache.getCache().getNavigationString().equals(""))
		{
			return this.reloadNavigationString();
		}
		return Cache.getCache().getNavigationString();
	}
	
	
	public String reloadNavigationString()
	{
		List<Lable> list=this.lableMapper.getNavigation(0);
		List<Lable> listsub;
		Lable l;
		StringBuffer sb=new StringBuffer();
		
		for(int i=0;i<list.size();i++)
		{
			l=list.get(i);
			
			sb.append("<span class=\"headerlables\">");
			
			sb.append("<a href=\"/action.htm?tid="+l.getId()+"\">"+l.getName()+"</a><div class=\"headerlablesflame\">");
			
			//查找本ID的子目录
			listsub=this.lableMapper.getNavigation(l.getId());
			
			for(int j=0;j<listsub.size();j++)
			{
				l=listsub.get(j);
				sb.append("<div class=\"headersublables\">");
				sb.append("<a href=\"/action.htm?tid="+l.getId()+"\">"+l.getName()+"</a>");
				sb.append("</div>");
			}
			
			
			sb.append("</div></span>");
			sb.append("\n");
			sb.append("\t\t");
			
		}
		
		Cache.getCache().setNavigationString(sb.toString());
		
		return sb.toString();
	}
}
